package org.example.Interceptors;

import org.example.common.ApiResponse;
import org.example.common.TokenStorage;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @Description
 * @Author 魏锴
 * @Date 2024/1/11
 */
public class testerInterceptor implements HandlerInterceptor {
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        if ("OPTIONS".equals(request.getMethod())) return true;
        System.out.println("<------ 测试员身份校验开始 ------>");
        String token = TokenStorage.getToken();//获取token
        Object obj = request.getSession().getAttribute(token + ":roles");
        if (obj != null) {
            List<Integer> roles = (List<Integer>) obj;
            if (roles.contains(3)) {
                System.out.println("<------ 测试员身份校验完成 ------>");
                return true;
            } else {
                ApiResponse.returnDataDirectly(response, ApiResponse.noPermission("测试员"));
                System.out.println("<------ 测试员身份校验失败 ------>");
                return false;
            }
        } else {
            System.out.println("<------ 测试员身份校验失败 ------>");
            ApiResponse.returnDataDirectly(response, ApiResponse.certificationExpires());
            return false;
        }
    }
}
